package com.xinliu.fastchat.util;

/*
 @author: xinliu
 @email: 3536370493@qq.com
 @time : 2025/9/23 1:17
*/

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.auth0.jwt.interfaces.Verification;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Map;
import java.util.Objects;

@Component
public class JWTUtils {

    private static String secret = "心流3d0b";

    private static Integer expired = 1;

    public static String build(Map<String, Object> claims) {
        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.DATE, expired);
        JWTCreator.Builder builder = JWT.create();
        builder.withPayload(claims);
        builder.withExpiresAt(instance.getTime());
        return builder.sign(Algorithm.HMAC256(secret));
    }

    public static DecodedJWT parse(String token) {
        Verification verification = JWT.require(Algorithm.HMAC256(secret));
        JWTVerifier verifier = verification.build();
        DecodedJWT verify = verifier.verify(token);
        return verify;
    }
}
